package io.coderyeah.org.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.basic.exception.BusinessException;
import io.coderyeah.basic.util.BaiduAuditUtils;
import io.coderyeah.org.domain.BaseDomain;
import io.coderyeah.org.domain.Employee;
import io.coderyeah.org.domain.Shop;
import io.coderyeah.org.domain.ShopAuditLog;
import io.coderyeah.org.dto.ShopDTO;
import io.coderyeah.org.mapper.EmployeeMapper;
import io.coderyeah.org.mapper.ShopAuditLogMapper;
import io.coderyeah.org.mapper.ShopMapper;
import io.coderyeah.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * @author coderyeah
 * @description 针对表【t_shop】的数据库操作Service实现
 * @createDate 2022-09-11 12:10:57
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper auditLogMapper;
    @Resource
    private JavaMailSender javaMailSender;

    @Transactional
    @Override
    public void settlement(Shop shop) {
        // 1.判断店铺名是否为空
        if (StrUtil.isBlank(shop.getName())) {
            throw new BusinessException("店铺数据不能为空!!!");
        }
        // 效验店铺名称是否违法
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名称不合法，请重新输入！！！");
        }
        // 效验店铺logo是否违法
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new BusinessException("图片不合法，请重新选择！！！");
        }
        // 2.判断店铺是否已经入住
        final LambdaQueryWrapper<Shop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shop::getName, shop.getName()).eq(Shop::getAddress, shop.getAddress());
        final Shop dbShop = shopMapper.selectOne(queryWrapper);
        if (dbShop != null) {
            // 说明店铺已经存在不能重复提交
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }
        // 3.保存店铺管理员信息
        final Employee admin = shop.getAdmin();
        employeeMapper.insert(admin);
        // 4.保存店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.insert(shop); // 返回自增长id
        // 5.更新店铺管理员的shopID信息
        admin.setShopId(shop.getId());
        employeeMapper.updateById(admin);
    }

    @Override
    public Result getAll(ShopDTO dto) {
        List<Shop> shops = shopMapper.getAll(dto);
        Long count = shopMapper.getCount(dto);
        return Result.success(shops, count);
    }

    @Transactional
    @Override
    public Result pass(ShopAuditLog auditLog) throws MessagingException {
        // 1.店铺审核通过 将t_shop表状态修改为2表示审核通过待激活
        final Shop shop = new Shop();
        shop.setState(2);
        shopMapper.update(shop, new LambdaQueryWrapper<Shop>().eq(BaseDomain::getId, auditLog.getShopId()));
        // 2. 新增审核日志
        auditLog.setAuditId(356L); // 审核人
        auditLog.setState(2);
        auditLogMapper.insert(auditLog);
        // 3.发送激活邮件 复杂邮件
        final MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2399690261@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active/" + auditLog.getShopId() + "'><font color='blue'>点击这里</font></a>激活邮件</h3>", true);
        final Shop shop1 = shopMapper.selectById(auditLog.getShopId()); // 待激活店铺
        // 店铺管理员
        final Employee employee = employeeMapper.selectById(shop1.getAdminId());
        helper.setTo(employee.getEmail()); //收件人
        // 发送邮件
        javaMailSender.send(mimeMessage);
        return Result.success(null);
    }


    @Transactional
    @Override
    public Result reject(ShopAuditLog auditLog) throws MessagingException {
        // 新增审核日志
        auditLog.setAuditId(356L); // 审核人
        auditLog.setState(4);
        auditLogMapper.insert(auditLog);

        // 修改店铺状态
        final Shop shop = new Shop();
        shop.setState(4);
        shopMapper.update(shop, new LambdaQueryWrapper<Shop>().eq(BaseDomain::getId, auditLog.getShopId()));

        // 发送驳回邮件
        final MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2399690261@qq.com");
        helper.setSubject("入驻失败邮件");
        final Shop shop1 = shopMapper.selectById(auditLog.getShopId()); // 待激活店铺
        //设置内容
        helper.setText("<h1>入驻失败!!!</h1>" + "<a href='http://localhost:8081/#/register'>点击重新入驻</a>", true);
        // 店铺管理员
        final Employee employee = employeeMapper.selectById(shop1.getAdminId());
        helper.setTo(employee.getEmail()); //收件人
        // 发送邮件
        javaMailSender.send(mimeMessage);
        // 删除店铺管理员
//        employeeMapper.deleteById(shop1.getAdminId());
        // 删除店铺信息
//        shopMapper.deleteById(shop1.getId());
        return Result.success(null);
    }

    @Override
    public List<ShopDTO> getEchartsData() {
        return shopMapper.getEchartsData();
    }

    @Override
    public Result active(Long shopId) {
        final ShopAuditLog auditLog = new ShopAuditLog();
        // 新增审核日志
        auditLog.setAuditId(356L); // 审核人
        auditLog.setState(3);
        auditLog.setShopId(shopId);
        auditLogMapper.insert(auditLog);
        // 修改店铺状态
        final Shop shop = new Shop();
        shop.setState(3);
        shopMapper.update(shop, new LambdaQueryWrapper<Shop>().eq(BaseDomain::getId, shopId));
        return Result.success(null);
    }
}




